﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MTC.Common.Configuration;
using MTC.Common.DTO;

namespace MTC.DataAccess
{
    public static class BodyTypeDataAccess
    {
        public static List<BodyType> GetBodyTypes()
        {
            List<BodyType> listOfBodyType = new List<BodyType>();

            Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);
            if (db != null)
            {
                const string sqlCommandText = "spBodyType_GetAllBodyTypes";
                using (SqlCommand sqlCommand = db.GetStoredProcCommand(sqlCommandText) as SqlCommand)
                {

                    using (SqlDataReader dataReader = db.ExecuteReader(sqlCommand) as SqlDataReader)
                    {
                        BodyType entity;

                        if (dataReader != null)
                            while (dataReader.Read())
                            {
                                entity = new BodyType();
                                if (!Convert.IsDBNull(dataReader["BodyTypeId"]))
                                    entity.BodyTypeId = Convert.ToInt32(dataReader["BodyTypeId"]);
                                if (!Convert.IsDBNull(dataReader["BodyDescription"]))
                                    entity.BodyDescription = Convert.ToString(dataReader["BodyDescription"]);

                                listOfBodyType.Add(entity);
                            }
                    }
                }
            }
            return listOfBodyType;
            
 
        }


        public static int Insert(BodyType bodytype)
        {
            Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);

            const string sqlCommand = "spBodyType_Insert";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);

            db.AddInParameter(dbCommand, "@BodyTypeDescription", DbType.String, bodytype.BodyDescription);
            return db.ExecuteNonQuery(dbCommand);

        }

        public static bool Update(BodyType bodytype)
        {
            int result = 0;
            try
            {
                Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);

                const string sqlCommandText = "spBodyType_Update";
                using (SqlCommand sqlCommand = db.GetStoredProcCommand(sqlCommandText) as SqlCommand)
                {
                    db.AddInParameter(sqlCommand, "@BodyTypeId", DbType.Int32, bodytype.BodyTypeId);
                    db.AddInParameter(sqlCommand, "@BodyTypeDescription", DbType.String, bodytype.BodyDescription);

                    result = db.ExecuteNonQuery(sqlCommand);
                }
  
            }
            catch(Exception e)
            {
                MTC.Common.Logging.Error(e.Message,e);
            }
            return result > 0 ? true : false;
        }

        public static bool Delete(BodyType bodytype)
        {
            Database db = DatabaseFactory.CreateDatabase(ApplicationSettings.Settings.Database.ConnectionStringName);

            const string sqlCommandText = "spBodyType_Delete";
            int result;
            using (SqlCommand sqlCommand = db.GetStoredProcCommand(sqlCommandText) as SqlCommand)
            {
                db.AddInParameter(sqlCommand, "@BodyTypeId", DbType.Int32, bodytype.BodyTypeId);
                result = db.ExecuteNonQuery(sqlCommand);
            }

            return result > 0 ? true : false;

        }
    }
}
